package com.ss.android.common.app;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.View;
import android.view.ViewGroup;

import com.bytedance.common.utility.collection.WeakList;
import com.ss.android.common.app.permission.OnRequestPermissionsResultCallback;
import com.ss.android.common.app.permission.PermissionsManager;
import com.ss.android.common.app.permission.RequestPermissionsRequestCodeValidator;
import com.ss.android.common.util.ImmersedStatusBarHelper;
import com.ss.android.newmedia.a.InputMethodManagerUtil;

import java.util.Iterator;
import java.util.Map;

public abstract class AbsActivity extends AppCompatActivity implements
        ActivityStackManager.StackRecorder,
        IComponent,
        LifeCycleInvoker,
        RequestPermissionsRequestCodeValidator,
        OnRequestPermissionsResultCallback {
    public static final String ACTION_EXIT_APP = "com.ss.android.common.app.action.exit_app";
    private static final String KEY = "abs_Activity_Key";
    private boolean mDisableOptimizeViewHierarchy;
    private BroadcastReceiver mExitAppReceiver;
    private ImmersedStatusBarHelper mImmersedStatusBarHelper;
    private String mKey;
    private WeakList<LifeCycleMonitor> mMonitors;
    public boolean mStatusActive;
    protected boolean mStatusDestroyed;

    public AbsActivity() {

        this.mStatusActive = false;
        this.mStatusDestroyed = false;
        this.mMonitors = new WeakList();
    }

    public boolean enableInitHook() {
        return true;
    }

    public boolean enableMobClick() {
        return true;
    }

    public Map getEnterEventContext() {
        return null;
    }

    public ImmersedStatusBarHelper.ImmersedStatusBarConfig getImmersedStatusBarConfig() {
        return new ImmersedStatusBarHelper.ImmersedStatusBarConfig();
    }

    public ImmersedStatusBarHelper getImmersedStatusBarHelper() {
        return this.mImmersedStatusBarHelper;
    }

    public Map getLeaveEventContext() {
        return null;
    }

    public String getRecorderKey() {
        return this.mKey;
    }

    public boolean getScreenSwitch() {
        return true;
    }

    public boolean isActive() {
        return this.mStatusActive;
    }

    public boolean isAppBackground() {
        return AppHooks.mAvailableActivityNum == 0;
    }

    public boolean isDestroyed() {
        if (Build.VERSION.SDK_INT < 17) {
            return this.mStatusDestroyed;
        }

        try {
            return super.isDestroyed();
        } catch (Throwable throwable) {
            return this.mStatusDestroyed;
        }
    }

    public boolean isViewValid() {
        return !this.mStatusDestroyed;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        AppHooks.ActivityResultHook activityResultHook = AppHooks.getActivityResultHook();
        if (activityResultHook == null
                || !activityResultHook.onActivityResult(this, requestCode, resultCode, intent)) {
            super.onActivityResult(requestCode, resultCode, intent);
        }
    }

    public void onContentChanged() {
        super.onContentChanged();
        if (this.mImmersedStatusBarHelper != null) {
            if (this.mImmersedStatusBarHelper.mFitsSystemWindows) {
                ViewGroup contentView = this.mImmersedStatusBarHelper.mActivity.findViewById(android.R.id.content);
                if (contentView != null) {
                    this.mImmersedStatusBarHelper.b = contentView.getChildAt(contentView.getChildCount() - 1);
                    boolean v0_1 = this.mImmersedStatusBarHelper.d;
                    if (Build.VERSION.SDK_INT >= 21
                            && (ImmersedStatusBarHelper.mIsEnable)
                            && this.mImmersedStatusBarHelper.b != null) {
                        View view = this.mImmersedStatusBarHelper.b;
                        int v0_2 = v0_1 ? this.mImmersedStatusBarHelper.c() : 0;
                        view.setPadding(0, v0_2, 0, 0);
                    }
                }
            }
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        try {
            this.mImmersedStatusBarHelper = new ImmersedStatusBarHelper(((Activity) this), this.getImmersedStatusBarConfig());
            this.mImmersedStatusBarHelper.setup();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        super.onCreate(savedInstanceState);
        if (com.bytedance.article.common.a.h.q() > 0) {
            if (System.currentTimeMillis() - com.bytedance.article.common.a.h.q() > 3000) {
                com.bytedance.article.common.a.h.d(0);
            }

            com.bytedance.article.common.a.h.e(0);
        }

        this.mKey = savedInstanceState == null || !savedInstanceState.containsKey("abs_Activity_Key") ? ActivityStackManager.buildKey(((Activity) this)) : savedInstanceState.getString("abs_Activity_Key");
        AppHooks.InitHook v0_1 = AppHooks.getInitHook();
        if (v0_1 != null && (this.enableInitHook())) {
            v0_1.tryInit(((Context) this));
        }

        this.mExitAppReceiver = new BroadcastReceiver() {
            @Override
            public final void onReceive(Context context, Intent intent) {
                if (!AbsActivity.this.isFinishing()) {
                    AbsActivity.this.finish();
                }
            }
        };
        LocalBroadcastManager.getInstance(this).registerReceiver(this.mExitAppReceiver,
                new IntentFilter("com.ss.android.common.app.action.exit_app"));
        ActivityStackManager.addRecorder(this);
        ++AppHooks.mForegroundActivityNum;
    }

    public void onDestroy() {
        LocalBroadcastManager.getInstance(((Context) this)).unregisterReceiver(this.mExitAppReceiver);
        super.onDestroy();
        InputMethodManagerUtil.fixMemoryLeak(this);
        this.mStatusDestroyed = true;
        if (!this.mMonitors.weakHashMap.isEmpty()) {
            Iterator v1 = this.mMonitors.iterator();
            while (v1.hasNext()) {
                Object v0 = v1.next();
                if (v0 == null) {
                    continue;
                }

                ((LifeCycleMonitor) v0).onDestroy();
            }

            this.mMonitors.weakHashMap.clear();
        }

        ActivityStackManager.removeRecorder(this);
        --AppHooks.mForegroundActivityNum;
    }

    public void onPause() {
        super.onPause();
        this.mStatusActive = false;
        AppHooks.ActivityLifeCycleHook activityLifeCycleHook = AppHooks.getActivityHook();
        if (activityLifeCycleHook != null) {
            activityLifeCycleHook.onActivityPaused(this);
        }

        if (!this.mMonitors.weakHashMap.isEmpty()) {
            Iterator<LifeCycleMonitor> iterator = this.mMonitors.iterator();
            while (iterator.hasNext()) {
                LifeCycleMonitor lifeCycleMonitor = iterator.next();
                if (lifeCycleMonitor != null) {
                    lifeCycleMonitor.onPause();
                }
            }
        }
    }

    public void onRequestPermissionsResult(int arg3, @NonNull String[] arg4, @NonNull int[] arg5) {
        super.onRequestPermissionsResult(arg3, arg4, arg5);
        if ((arg3 >> 8 & 255) == 0) {
            PermissionsManager.getInstance().notifyPermissionsChange(this, arg4, arg5, this.showSelfPermissionDialog());
        }
    }

    protected void onRestoreInstanceState(Bundle arg2) {
        super.onRestoreInstanceState(arg2);
        if (arg2 != null && (arg2.containsKey("abs_Activity_Key"))) {
            this.mKey = arg2.getString("abs_Activity_Key");
        }
    }

    public void onResume() {
        super.onResume();
        this.mStatusActive = true;
        AppHooks.AppStartMonitorHook appStartMonitorHook = AppHooks.getAppStartMonitorHook();
        if (appStartMonitorHook != null && com.bytedance.article.common.a.h.l() > 0) {
            appStartMonitorHook.onAppStartMonitor();
        }

        AppHooks.ActivityLifeCycleHook activityLifeCycleHook = AppHooks.getActivityHook();
        if (activityLifeCycleHook != null) {
            activityLifeCycleHook.onActivityResumed( this);
        }

        if (!this.mMonitors.weakHashMap.isEmpty()) {
            Iterator<LifeCycleMonitor> iterator = this.mMonitors.iterator();
            while (iterator.hasNext()) {
                LifeCycleMonitor lifeCycleMonitor = iterator.next();
                if (lifeCycleMonitor != null) {
                    lifeCycleMonitor.onResume();
                }
            }
        }
    }

    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (outState != null) {
            outState.putString("abs_Activity_Key", this.mKey);
            outState.putBoolean("WORKAROUND_FOR_BUG_19917_KEY", true);
        }
    }

    public void onStart() {
        super.onStart();
        if (AppHooks.mAvailableActivityNum == 0) {
            AppHooks.AppBackgroundHook appBackgroundHook = AppHooks.getAppBackgroundHook();
            if (appBackgroundHook != null) {
                appBackgroundHook.onAppBackgroundSwitch(false);
            }
        }
        ++AppHooks.mAvailableActivityNum;
    }

    public void onStop() {
        super.onStop();
        int v0 = AppHooks.mAvailableActivityNum - 1;
        AppHooks.mAvailableActivityNum = v0;
        if (v0 == 0) {
            AppHooks.AppBackgroundHook appBackgroundHook = AppHooks.getAppBackgroundHook();
            if (appBackgroundHook != null) {
                appBackgroundHook.onAppBackgroundSwitch(true);
            }
        }

        this.mStatusActive = false;
        if (!this.mMonitors.weakHashMap.isEmpty()) {
            Iterator<LifeCycleMonitor> iterator = this.mMonitors.iterator();
            while (iterator.hasNext()) {
                LifeCycleMonitor lifeCycleMonitor = iterator.next();
                if (lifeCycleMonitor != null) {
                    lifeCycleMonitor.onStop();
                }
            }
        }
    }

    public void registerLifeCycleMonitor(LifeCycleMonitor lifeCycleMonitor) {
        this.mMonitors.add(lifeCycleMonitor);
    }

    public void requestDisableOptimizeViewHierarchy() {
        this.mDisableOptimizeViewHierarchy = true;
    }

    public boolean showSelfPermissionDialog() {
        return true;
    }

    public void unregisterLifeCycleMonitor(LifeCycleMonitor lifeCycleMonitor) {
        this.mMonitors.remove(lifeCycleMonitor);
    }
}

